package com.jsbb.util;

import org.springframework.core.env.Environment;

/**
 * Spring Environment，用于静态获取配置参数
 *
 * @author w
 * @date 2021-03-05
 */
public class EnvironmentUtils {

    private static Environment environment = SpringContextUtils.getBean(Environment.class);

    public static String getProperty(String name) {
        return environment.getProperty(name);
    }

    public static String getDsIp() {
        String url = environment.getProperty("spring.datasource.url");
        return url.substring(url.indexOf("//") + 2, url.lastIndexOf(":"));
    }

    public static String getDsPort() {
        String url = environment.getProperty("spring.datasource.url");
        return url.substring(url.lastIndexOf(":") + 1, url.lastIndexOf("/"));
    }

    public static String getDsSchema() {
        String url = environment.getProperty("spring.datasource.url");
        return url.substring(url.lastIndexOf("/") + 1, url.indexOf("?"));
    }

    public static String getDsUser() {
        return environment.getProperty("spring.datasource.username");
    }

    public static String getDsPassword() {
        return environment.getProperty("spring.datasource.password");
    }
}
